package com.example.flashsales.controller;

import com.example.flashsales.entity.Result;
import com.example.flashsales.entity.vo.OrdersVo;
import com.example.flashsales.service.impl.OrderServiceImpl;
import com.example.flashsales.service.impl.StoreServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/store/order")
public class StoreOrderController {

    @Autowired
    StoreServiceImpl storeService;

    @Autowired
    OrderServiceImpl orderService;


    /**
     * 查看所有订单
     * @param httpServletRequest
     * @param num
     * @param page
     * @return
     */
    @ResponseBody
    @GetMapping("/viewAllOrders")
    public Result viewAllOrders(HttpServletRequest httpServletRequest,int num,int page){
        long id = Integer.parseInt(httpServletRequest.getAttribute("id") + "");
        if (page <= 0){
            return Result.error("页数需要大于等于1");
        }
        final List<OrdersVo> ordersVoList = storeService.viewAllOrders(id, page, num);
        Map<String,Object> map = new HashMap<>();
        final long l = orderService.viewStoreOrderCount(id);
        map.put("total",l);
        map.put("data",ordersVoList);
        return Result.ok("查看成功").setData(map);
    }


    /**
     * 按时间查看订单
     * @param httpServletRequest
     * @param qDate
     * @param hDate
     * @return
     */
    @ResponseBody
    @GetMapping("/viewOrdersByTime")
    public Result viewOrdersByTime(HttpServletRequest httpServletRequest, Date qDate,Date hDate,int num,int page){
        long id = Integer.parseInt(httpServletRequest.getAttribute("id") + "");
        if (page <= 0){
            return Result.error("页数需要大于等于1");
        }
        final List<OrdersVo> ordersVoList = storeService.viewOrdersByTime(id,page,num,qDate,hDate);
        Map<String,Object> map = new HashMap<>();
        final long l = orderService.viewStoreOrderCountByTime(id,qDate,hDate);
        map.put("total",l);
        map.put("data",ordersVoList);
        return Result.ok("查看成功").setData(map);
    }


    /**
     * 确认收货
     * @param httpServletRequest
     * @param orderId
     * @return
     */
    @ResponseBody
    @GetMapping("/updateOrderState")
    public Result updateOrderState(HttpServletRequest httpServletRequest,long orderId){
        long id = Integer.parseInt(httpServletRequest.getAttribute("id") + "");
        final List<OrdersVo> ordersVoList = orderService.viewOrder(orderId);
        if (ordersVoList.isEmpty()){
            return Result.error("订单不存在");
        }
        if (ordersVoList.get(0).getState() != 0) {
            return Result.error("订单未到达小店");
        }
        final boolean b = storeService.updateOrderState(orderId, id);
        if (b){
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }


    /**
     * 修改订单到达小店
     * @param orderId
     * @return
     */
    @ResponseBody
    @GetMapping("/updateOrderByArrive")
    public Result updateOrderByArrive(Long orderId){
        if(orderId == null){
            return Result.error("数据不能为空");
        }
        final List<OrdersVo> ordersVoList = orderService.viewOrder(orderId);
        if (ordersVoList.isEmpty()){
            return Result.error("订单不存在");
        }
        if (ordersVoList.get(0).getState() != 3) {
            return Result.error("订单未分拣");
        }
        if (orderService.updateOrderByArrive(orderId)) {
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }

}

